import { RepositorySearchResultList } from './api'

export async function search (keyword:string, page:number) {
  const url = new URL('https://api.github.com/search/repositories')
  url.searchParams.append('q', keyword)
  url.searchParams.append('page', page.toString())
  const response = await fetch(url.toString(), { method: 'get' })

  const result = await response.json()

  if (response.status >= 400) {
    throw new Error(result.message)
  }

  return result as RepositorySearchResultList
}
